/*
    XHoldingPattern
    Copyright (C) 2009 Mike Gehl

    X-Plane Plugin SDK
    Copyright (C) Sandy Barbour and Ben Supnik

    This file is part of XHoldingPattern.

    XHoldingPattern is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    XHoldingPattern is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with XHoldingPattern.  If not, see <http://www.gnu.org/licenses/>.
    ---------------------------------------------------------------------------
    about.c
    Copyright (C) Mike Gehl 2009 <mjgehl@gmail.com>

    Display an about dialog.
*/


#include <stdio.h>
#include <string.h>

#include "XPLMDataAccess.h"
#include "XPLMProcessing.h"
#include "XPWidgets.h"
#include "XPStandardWidgets.h"
#include "XPLMGraphics.h"

#include "about.h"
#include "xHoldPattern.h"

/* Global About Window Widget */
XPWidgetID gAbout_dialog_widget = NULL;

void holdpat_about_dialog(void)
{
    int x_center =  XPLMGetDatai(XPLMFindDataRef("sim/graphics/view/window_width")) / 2;
    int y_center =  XPLMGetDatai(XPLMFindDataRef("sim/graphics/view/window_height")) / 2;
    int xx = x_center - (ABOUT_WIDTH / 2);
    int yy = y_center + (ABOUT_HEIGHT / 1.3);
    char str[256];

	/* Create the Main Widget window */
	sprintf(str, "About %s", PRG_NAME);
	gAbout_dialog_widget = XPCreateWidget(xx,
                    yy,
                    xx + ABOUT_WIDTH,
                    yy - ABOUT_HEIGHT,
                    1,	// Visible
                    str,	// desc
                    1,		// root
                    NULL,	// no container
                    xpWidgetClass_MainWindow);

	/* Add Close Box decorations to the Main Widget */
	XPSetWidgetProperty(gAbout_dialog_widget, xpProperty_MainWindowHasCloseBoxes, 1);


    /* Create the Sub Widget window */
	XPWidgetID about_dialog_window = XPCreateWidget(xx + 10,
                    yy - 30,
                    xx + ABOUT_WIDTH - 10,
                    yy - ABOUT_HEIGHT + 40,
                    1,	// Visible
                    "",	// desc
                    0,	// root
                    gAbout_dialog_widget,
                    xpWidgetClass_SubWindow);

	/* Set the style to sub window */
	XPSetWidgetProperty(about_dialog_window, xpProperty_SubWindowType, xpSubWindowStyle_SubWindow);


    /* Create a Close Button */
	XPWidgetID about_close_btn = XPCreateWidget(x_center - 30,
                    yy - ABOUT_HEIGHT + 30,
                    x_center + 30,
                    yy - ABOUT_HEIGHT + 15,
					1, "Close",
					0,
					gAbout_dialog_widget,
					xpWidgetClass_Button);

	XPSetWidgetProperty(about_close_btn, xpProperty_ButtonType, xpPushButton);



    /* Create About Text */

    sprintf(str, "%s %s", PRG_NAME, PRG_VERSION);
    float ww = center_text(str, x_center, &xx);
	XPCreateWidget(xx, yy - 40, xx + ww, yy - 52,
					1, str,
					0,
					gAbout_dialog_widget,
					xpWidgetClass_Caption);

    strcpy(str, "A holding pattern automatation utility.");
    ww = center_text(str, x_center, &xx);
	XPCreateWidget(xx, yy - 70, xx + ww, yy - 82,
					1, str,
					0,
					gAbout_dialog_widget,
					xpWidgetClass_Caption);

    strcpy(str, "http://code.google.com/p/xholdingpattern/");
    ww = center_text(str, x_center, &xx);
	XPCreateWidget(xx, yy - 82, xx + ww, yy - 94,
					1, str,
					0,
					gAbout_dialog_widget,
					xpWidgetClass_Caption);

    strcpy(str, "XHoldingPattern is free software: you can redistribute it and/or modify");
    ww = center_text(str, x_center, &xx);
	XPCreateWidget(xx, yy - 120, xx + ww, yy - 132,
					1, str,
					0,
					gAbout_dialog_widget,
					xpWidgetClass_Caption);

    strcpy(str, "it under the terms of the GNU General Public License as published by");
    ww = center_text(str, x_center, &xx);
	XPCreateWidget(xx, yy - 132, xx + ww, yy - 144,
					1, str,
					0,
					gAbout_dialog_widget,
					xpWidgetClass_Caption);

    strcpy(str, "the Free Software Foundation.");
    ww = center_text(str, x_center, &xx);
	XPCreateWidget(xx, yy - 144, xx + ww, yy - 156,
					1, str,
					0,
					gAbout_dialog_widget,
					xpWidgetClass_Caption);

    strcpy(str, PRG_COPYRIGHT);
    ww = center_text(str, x_center, &xx);
	XPCreateWidget(xx, yy - ABOUT_HEIGHT + 65, xx + ww, yy - ABOUT_HEIGHT + 53,
					1, str,
					0,
					gAbout_dialog_widget,
					xpWidgetClass_Caption);;

	// Register our widget handler
	XPAddWidgetCallback(gAbout_dialog_widget, about_dialog_handler);
	XPAddWidgetCallback(about_close_btn, about_dialog_handler);
}


int about_dialog_handler(XPWidgetMessage inMessage,
                    XPWidgetID inWidget,
                    long inParam1,
                    long inParam2)
{
    if (gAbout_dialog_widget &&
        (inMessage == xpMessage_CloseButtonPushed ||
        inMessage == xpMsg_PushButtonPressed))
	{
		XPDestroyWidget(gAbout_dialog_widget, 1);
		gAbout_dialog_widget = NULL;
		return 1;
	}
	return 0;
}


float center_text(char *str, int center, int *x_left)
{
    float ww = XPLMMeasureString(xplmFont_Proportional, str, strlen(str));
    *x_left = center - (ww / 2.0);
    return ww;
}
